package com.neu.cpsp.service.impl;

import com.neu.cpsp.dao.RegulationDetailMapper;
import com.neu.cpsp.dao.RegulationTimesMapper;
import com.neu.cpsp.pojo.RegulationDetail;
import com.neu.cpsp.pojo.RegulationDetailAndTimes;
import com.neu.cpsp.pojo.RegulationTimes;
import com.neu.cpsp.service.RegulationDetailAndTimesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegulationDetailAndTimesServiceImpl implements RegulationDetailAndTimesService {

    @Autowired
    RegulationDetailMapper regulationDetailMapper;

    @Autowired
    RegulationTimesMapper regulationTimesMapper;

    @Override
    public List<RegulationDetailAndTimes> getTypeAndDetail(RegulationDetail record) {
        return regulationDetailMapper.selectDetailAndTimes(record);
    }

    @Override
    public int maxTimesByDetailId(Integer regulationDetailId) {
        return regulationTimesMapper.maxTimesByDetailId(regulationDetailId);
    }

    @Override
    public int submitForm(RegulationDetailAndTimes record) throws Exception {
        int flag;
        RegulationDetail regulationDetail = new RegulationDetail();
        regulationDetail.setRegulationTypeId(record.getRegulationTypeId());
        regulationDetail.setCode(record.getCode());
        regulationDetail.setDescribe(record.getDescribe());
        List<RegulationTimes> regulationTimesList = record.getRegulationTimes();
        try {
            regulationDetailMapper.insertSelective(regulationDetail);
            for (RegulationTimes regulationTimes : regulationTimesList){
                regulationTimes.setRegulationDetailId(regulationDetail.getId());  //detail表返回的id赋值给times表的记录
                regulationTimesMapper.insertSelective(regulationTimes);
            }
            flag = 1;
        }catch (Exception e){
            throw new Exception("添加记录出现异常");
        }
        return flag;
    }

    @Override
    public int delete(Integer id) throws Exception {
        int flag;
        try {
            regulationDetailMapper.deleteByPrimaryKey(id);
            regulationTimesMapper.deleteByRegulationDetailId(id);
            flag = 1;
        }catch (Exception e){
            throw new Exception("删除记录出现异常");
        }
        return flag;
    }

    @Override
    public int update(RegulationDetailAndTimes record) throws Exception {
        int flag;
        RegulationDetail regulationDetail = new RegulationDetail();
        regulationDetail.setId(record.getId());
        regulationDetail.setRegulationTypeId(record.getRegulationTypeId());
        regulationDetail.setCode(record.getCode());
        regulationDetail.setDescribe(record.getDescribe());
        List<RegulationTimes> regulationTimesList = record.getRegulationTimes();
        try {
            regulationDetailMapper.updateByPrimaryKeySelective(regulationDetail);
            regulationTimesMapper.deleteByRegulationDetailId(record.getId());
            for (RegulationTimes regulationTimes : regulationTimesList){
                regulationTimes.setRegulationDetailId(record.getId());
                regulationTimesMapper.insertSelective(regulationTimes);
            }
            flag = 1;
        }catch (Exception e){
            throw new Exception("修改记录出现异常");
        }
        return flag;
    }
}
